define([
    'jquery',
    'underscore',
    'backbone',
    ], function($, _, Backbone){
        var $html_loadding = '<div class="caculation-report" style="text-align:center"><img src="'+$template_admincp_url+'images/preloaders/blue/4.gif"/></div>';
        var pageView = Backbone.View.extend({
            el: $("#main"),
            initialize: function(){
				
            },
            events: {
                
            },
            
            view: function(){
                var url = location.href;
                if(typeof oTable != "undefined"){
                    $("#table_page").dataTable().fnDestroy();
                }
                oTable = $('#table_page').dataTable({
                   "sDom": "<'row'<'col-lg-8'l><'col-lg-1 button_create'><'col-lg-3'f>r>t<'row'<'col-lg-6'i><'col-lg-6'p>>",
                    "sPaginationType": "full_numbers",
                    "iDisplayLength": 10,
                    "iDisplayStart": 0,
                    "bProcessing": true,
                    "bRetrieve": true,
                    "bServerSide": true,
                    "sAjaxSource": url,
                    "fnServerData": function(sSource, aoData, fnCallback, oSettings) {
                        if(typeof filters != "undefined"){
                            obj_tmp = new Object;
                            $.each(filters, function(name, value){
                                obj_tmp[name] = value;
                            });
                            aoData.push({
                                name: "myFilter",
                                value: JSON.stringify(obj_tmp)
                            });
                        }
                        $.ajax( {
                            "dataType": 'json',
                            "type": "POST",
                            "url": sSource,
                            "data": aoData,
                            success: function(rs){
                                ids = rs.my_id;
                                fnCallback(rs);
                            }
                        });

                    },
                    "aoColumns": [
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "title"
                        },
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "url_retail"
                        },
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "url_routes"
                        },
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "date_add"
                        },
                        {
                            // "mData": "action",
                            "sType": "string",
                            "swidth": "3%",
                            "sClass": "center",
                            "bSortable": false
                        }
                    ],

                    fnInitComplete: function()
                    {
                        $(this).slideDown(200);
                        $(".button_create").html('<button class="btn btn-primary btn-create" type="button">Create New</button>');
                        $(".btn-create").click(function(){
                            window.location.href = $admincp_url+'page/add';
                        });
                    }
                });
            },

            add: function(){
                $('#field_catalogue').hide();
                $('#type').select2({placeholder: "Select Type"});
                $('#catalogue').select2({placeholder: "Select Type"});
                $("#type").change(function(){
                    var type_id = $(this).val();
                    if(type_id == 1){
                        $('#field_catalogue').show();
                    }
                });
                $('#parent').select2({placeholder: "Select Parent"});
                $("#parent").change(function(){
                    var parent_id = $(this).val();
                    if(parent_id != 0){
                        $.ajax({
                            url: $base_url+'admincp/page/process',
                            type: 'post',
                            data: 'parent_id='+parent_id,
                            dataType: 'json',
                            async: false,
                            success: function(rs){
                                if(rs.length != 0){
                                    $("#url_retail").val(rs['url_retail']);
                                    $("#url_routes").val(rs['url_routes']);
                                }
                            }
                        });
                    }
                });
            },

            edit: function(){
                $('#field_catalogue').hide();
                $('#type').select2({placeholder: "Select Type"});
                $('#catalogue').select2({placeholder: "Select Type"});
                var type_id = $('#type').val();
                if(type_id == 1){
                    $('#field_catalogue').show();
                }
                $("#type").change(function(){
                    var type_id = $(this).val();
                    if(type_id == 1){
                        $('#field_catalogue').show();
                    }
                });
                $('#parent').select2({placeholder: "Select Parent"});
                $("#parent").change(function(){
                    var parent_id = $(this).val();
                    if(parent_id != 0){
                        $.ajax({
                            url: $base_url+'admincp/page/process',
                            type: 'post',
                            data: 'parent_id='+parent_id,
                            dataType: 'json',
                            async: false,
                            success: function(rs){
                                if(rs.length != 0){
                                    $("#url_retail").val(rs['url_retail']);
                                    $("#url_routes").val(rs['url_routes']);
                                }
                            }
                        });
                    }
                });
            },

            render: function(){
                if(typeof this[$app.action] != 'undefined'){
                    new this[$app.action];
                }
            }
        });
        return pageView = new pageView;
    });
